<?php

define(C("ACCESSACTION"), true);
import('jinghuan.AdminAction');

/**
 * Class ApiAction
 */
class BannerAction extends AdminAction
{

    function datatable()
    {
        $page = I('post.page');
        $length = I('post.length');
        $order = I('post.order');
        $echo = I('post.echo');
        $where = I('post.where');

        $count = D(JH_BANNER)->count();
        $row = D(JH_BANNER)
            ->page($page, $length)
            ->where($where)
            ->order($order)
            ->select();
        $this->callback(array(
            'rows' => $row,
            'echo' => $echo,
            'count' => $count
        ));
    }

    function edit()
    {
        $data = D(JH_BANNER)->find(intval($_POST['id']));
        $this->callback($data);
    }

    function do_edit()
    {
        $id = I('post.id');
        $title = I('post.title');
        $url = I('post.url');

        D(JH_BANNER)->save(array(
            id => $id,
            title => $title,
//            url => $url,
            u_time => time()
        ));
        $data = D(JH_BANNER)->find(intval($_POST['id']));
        $this->callback($data);
    }

    /**
     *
     */
    function do_change_status()
    {
        $id = I('post.id');
        $row = D(JH_BANNER)
            ->where(array('id' => $id))
            ->setField('status', array('exp', 'abs(`status` - 1)'));
        if ($row === false) {
            $this->callback(false);
            return;
        }

        $data = D(JH_BANNER)->find(intval($_POST['id']));
        $this->callback($data);
    }

    /**
     *
     */
    function do_add()
    {
        $post = I('post.');

        $result = D(JH_BANNER)->add(array(
            "title" => $post['title'],
//            "url" => $post['url'],
            "time" => time(),
            "user_id" => $_SESSION['admin_user_id'],
            "status" => 0
        ));
        $this->callback($result != false);
    }


    /**
     *
     */
    function do_change_sort()
    {
        $id = I('post.id');
        $sort = I('post.sort');
        $row = D(JH_BANNER)
            ->where(array('id' => $id))
            ->setField('sort', $sort);
        if ($row === false) {
            $this->callback(false);
            return;
        }

        $data = D(JH_BANNER)->find($id);
        $this->callback($data);
    }

}